/*
 * @Description: 
 * @Autor: 赵子惠
 * @Date: 2021-09-03 09:55:11
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-17 20:28:46
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { ProductionAssignmentServiceProxy, ProductionReportServiceProxy, ManHourInput, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize, filter } from 'rxjs/operators';
import { FileDownloadService } from '@shared/utils/file-download.service';
import * as moment from 'moment';


@Component({
    selector: 'app-team-management',
    templateUrl: './team-management.component.html',
    styleUrls: ['./team-management.component.less']
})
export class TeamManagementComponent extends AppComponentBase {

    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    model = undefined;
    modelOption = [];
    lotNo = undefined;
    LotNoList = [];
    organiza = undefined;
    organizationUnitList = [];
    rangeDates: Date[];
    barData1 = {}
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _productionAssignmentServiceProxy: ProductionAssignmentServiceProxy,
        private _productionReportServiceProxy: ProductionReportServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result;
            })
    }
    onChange(event) {
        //发次
        this._commonServiceProxy.getLotNoList(
            this.model.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.LotNoList = result;
            })
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._productionReportServiceProxy.getStatisticalGroupHour(
            this.model ? this.model.label : undefined,
            this.lotNo ? this.lotNo.label : undefined,
            this.model ? this.model.value : undefined,
            this.lotNo ? this.lotNo.value : undefined,
            this.organiza,
            undefined,
            this.rangeDates ? moment(this.rangeDates[0]) : undefined,
            this.rangeDates ? moment(this.rangeDates[1]) : undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            // this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result;
            this.primengTableHelper.hideLoadingIndicator();
            if (result.length == 0) return
            let labels = [], dataSetsRhour = [], dataSetsWhour = [];
            result.forEach(item => {
                labels.push(item.userName)
                dataSetsRhour.push(item.sumReportHour)
                dataSetsWhour.push(item.sumWorkingHour)
            })
            this.barData1 = {
                labels: labels,
                datasets: [
                    {
                        label: '任务数',
                        backgroundColor: '#42A5F5',
                        borderColor: '#1E88E5',
                        data: dataSetsRhour,
                    },
                    {
                        label: '工时提报',
                        backgroundColor: '#9CCC65',
                        borderColor: '#7CB342',
                        data: dataSetsWhour,
                    }
                ]
            }
        });
    }
    //获取左侧型号
    getModelList() {
        this.modelOption = []
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result
            })
    }
    //根据型号获取对应的发次
    // getLotNoList(e) {
    //     this.organizationUnitList = []
    //     this.organiza = undefined;
    //     this._commonServiceProxy.getLotNoList()
    //         .pipe(finalize(() => { })).subscribe(result => {
    //             this.LotNoList = result
    //         })
    // }
    // 获取班组
    getOrganizationUnit() {
        this._productionAssignmentServiceProxy.getOrganizationUnit(
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                result.forEach(item => {
                    this.organizationUnitList.push({
                        label: item.displayName,
                        value: item.id
                    })
                })
            });
    }
    reset() {
        this.model = undefined;
        this.lotNo = undefined;
        this.organiza = undefined;
        let dateE: Date[]
        this.rangeDates = dateE
    }
    outExcel() {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
        let dto = new ManHourInput();
        this._productionReportServiceProxy.exportGroupHour(
            dto)
            .subscribe(result => {
                // this.saveFlag = true;
                // this._fileDownloadService.downloadTempFile(result);
                window.open(result);
                URL.revokeObjectURL(result);
            });
        // }
    }

}
